/**
 * @file iris404_cmd.c
 * @author xlj
 * @brief iris404 integrated ops, including ops in shell.c but remove relevant parameters read from console
 * @version 0.1
 * @date 2023-05-11
 * 
 * @copyright Copyright (c) 2023
 * 
 */

#include "iris404_cmd.h"

void cmd_standby_idle(void)
{
    reg_write(0x08, 0x1800000);
    reg_write(0x20, 0x1000040);
    reg_write(0x1C, 0x1BF7FB8);
    reg_write(0x03, 0x1E10000);
}

void cmd_idle_fdidle(void)
{
	uint8_t i;
	uint32_t spidata;

	for(i=0;i<6;i++)
	{
		spidata = seq_idle2fdidle[i].ucValue;
		xspi_write_data(NODE_2,SBX_SPI_CSA,spidata,32);
		if(seq_idle2fdidle[i].ucValue == 0x04)
			wait_ms(1);
	}
}

void cmd_start_fdrx(void)
{
	reg_write(0x09, 0x1F2C260);
	reg_write(0x0F, 0x113B13B);
	reg_write(0x0E, 0x870000);
	reg_write(0x17, 0x800000);
	mac_start(200,0,0);
	wait_ms(1);
}

void cmd_stop_fdrx(void)
{
	mac_start(218,0,0);
	wait_ms(1);
	printf("state changed, stop fdrx \n");
}

void cmd_start_fdtx(void)
{
    reg_write(0x01, 0x980A);
    reg_write(0x05, 0x13B13B1);
    reg_write(0x04, 0x800000);
    reg_write(0x16, 0x800000);
    mac_start(223,0,0);
    wait_ms(1);
    reg_write(0x2D, 0x1A6F440);
    reg_write(0x1D, 0xF404C);
    reg_write(0x1D, 0xF5C00);
}

void cmd_stop_fdtx(void)
{
	mac_start(240,0,0);
    wait_ms(1);
	reg_write(0x2D, 0x126F440);
	reg_write(0x1D, 0xF4000);
	printf("state changed, stop fdtx \n");
}

void cmd_set_default_freq(void)
{
    // Default setting
    reg_write(0x4,0x08A0000); // ACP Band8 Tx freq 897MHz
    reg_write(0x5,0x0000000);
    reg_write(0x0F,0x1D89D89); // ACP Band8 Rx freq 942MHz
    reg_write(0x0E,0x0900000);
}

void setupTxLTE15()
{
	reg_write(0x0, 0x1F34E92);
	reg_write(0x2, 0x0DBF702);
	reg_write(0x16, 0x11C640C);
	reg_write(0x20, 0x1C22070);
	reg_write(0x22, 0x0050B2C);
	reg_write(0x2D, 0x1ADEF38);
    printf("[RF] Set tx bw to LTE15\n");
}

void setupTxLTE10()
{
	reg_write(0x0, 0x1F34EA2);
	reg_write(0x2, 0x0DBF702);
	reg_write(0x16, 0x11C640C);
	reg_write(0x20, 0x1C02270);
	reg_write(0x22, 0x0050B2C);
	reg_write(0x2D, 0x1AD6B28);
    printf("[RF] Set tx bw to LTE10\n");
}

void cmd_tx_bw(int bw)
{
    switch (bw)
    {
    case 10:
        setupTxLTE10();
        break;
    case 15:
        setupTxLTE15();
        break;

    default:
		printf("\n[ERR] current bandwidth is not supported :(\n");
        break;
    }
    osFastPause(1000);
}

void setupRxLTE3 ()
{
   reg_write(0xA,0xE7C825); // 0xEC4825
   reg_write(0xB,0xCB28C7); 
   reg_write(0x12,0x15121C);
   reg_write(0x20,0x1C2E670);
   reg_write(0x24,0x2E8BC0);
   reg_write(0x2A,0x174D558);
   printf("[RF] Set rx bw to LTE3\n");
}

void setupRxLTE10()
{
	reg_write(0xA,  0xEF4825);    //diff to ruan
    reg_write(0xB, 0xCA28C7);    //same
    reg_write(0x12, 0xD2C97);
    reg_write(0x20, 0x1C02270);
    //reg_write(0x24, 0x1E8B80);
    reg_write(0x2A, 0x17CCCE8);
    //new addition
    reg_write(0x2D, 0x1AD6B28);
    printf("[RF] Set rx bw to LTE10\n");
}

void cmd_rx_bw(int bw)
{
    switch (bw)
    {
    case 10:
        setupRxLTE10();
        break;
    case 3:
        setupRxLTE3();
        break;

    default:
		printf("\n[ERR] current bandwidth is not supported :(\n");
        break;
    }
    osFastPause(1000);
}

